<template>
  <div>
    <!-- 头部 -->
    <div class="header">
      <!-- <van-nav-bar title="账号密码登录"  left-arrow /> -->
      <div class="top-bar">
        <div class="back">
          <router-link to="/home">
            <i class="fa fa-angle-left fa-lg" style="color: black"></i>
          </router-link>
        </div>
        <div class="title">账号密码登录</div>
        <div class="register"></div>
      </div>
    </div>
    <div class="gap"></div>
    <!-- input框 -->
    <van-form>
      <van-field v-model="phone" name="phone" label="账号" placeholder="请输入账号" :rules="[
              {
                validator: rulesTel,
                trigger: 'onBlur',
              },
            ]">
      </van-field>
      <van-field v-model="password" type="password" name="password" center clearable label="密码" placeholder="请输入密码" :rules="[
                    {
                      validator: rulesPassword,
                      trigger: 'onBlur',
                    },
                  ]">
      </van-field>
      <!-- 登录按钮 -->
      <div style="margin: 16px;">
        <van-button type="warning info" block native-type="submit" @click="onLogin">登录</van-button>
      </div>
    </van-form>
    <!-- 忘记密码 -->
    <div class="forget-password">
      <router-link to="/lookfor">忘记密码</router-link>
    </div>
    <!-- 条款 -->
    <div class="agreement">
      <input type="checkbox" id="checkbox" v-model="checked" />
      <label for="checkbox"></label>
      <div class="agreement-text">
        我已阅读并同意<a href="https://rules-center.meituan.com/m/detail/guize/4?activeRule=1">《美团用户服务协议》</a>、
        <a href="https://rules-center.meituan.com/m/detail/guize/2?activeRule=1">《隐私政策》</a>等，接受免除或者限制责任、诉讼管辖约定等粗体标示条款
      </div>
    </div>
  </div>
</template>

<script>
  import {
    Toast
  } from 'vant';
  export default {
    name: "Login",
    data() {
      return {
        phone: "", // 手机号
        password: "", //密码
        checked: false, //用户协议
        token: '',
        flag: 1,
      };
    },
    methods: {
      toHome(){
        this.$router.push("/home");
      },
      // 校验账号
      rulesTel(val, rule) {
        if (!val) {
          rule.message = '';
          return false;
        }
        if (val.length < 4 || val.length > 20) {
          rule.message = '请输入有效账号';
          return false;
        }
        return true;
      },
      // 校验密码
      rulesPassword(val, rule) {
        if (!val) {
          rule.message = '';
          return false;
        }
        if (val.length < 4) {
          rule.message = '密码长度不能小于4位';
          return false;
        }
        if (val.length > 20) {
          rule.message = '密码长度不能大于20位';
          return false;
        }
        return true;
      },
      async onLogin() {
        const data = {
          phone: this.phone,
          password: this.password,
          flag: this.flag,
        }
        const {
          checked
        } = this;
        if (!checked) {
          this.$toast('请仔细阅读用户协议!');
          return;
        }

        const result = await this.$API.login(data);
        if (result.code === 200) {
          localStorage.setItem('token', result.token);
          Toast.loading('登录成功');
          this.token = result.token
          setTimeout(() => {
            Toast.clear();
            this.$router.push({
              path: '/home'
            })
          }, 1000)
        } else {
          Toast(result.msg);
        }
      }
    }
  }
</script>

<style scoped lang="less">
  .header {
    text-align: center;
    background: #fcfcfc;
    border-bottom: 1px solid #f63;
    padding: 0 15px;

    .top-bar {
      display: -webkit-inline-box;
      -webkit-box-pack: justify;
      -webkit-box-align: center;
      line-height: 45px;
      width: 100%;

      .back {
        display: block;
        -webkit-box-flex: 1;
        text-align: left;

        a {
          display: block;
          width: 100%;
          height: 45px;
          position: relative;
          text-decoration: none;
        }
      }

      .title {
        font-size: 18px;
        -webkit-box-flex: 3;
        text-align: center;
      }

      .register {
        -webkit-box-flex: 1;
      }
    }
  }

  van-field--error .van-field__control,
  .van-field--error .van-field__control::placeholder {
    color: #666;
  }

  .gap {
    height: 10px;
  }

  .form-info {
    line-height: 12px;
    margin-top: 5px;
    text-align: center;
    color: #999;
  }

  .forget-password {
    text-align: left;
    margin-top: 14.5px;
    margin-left: 15px;
    display: block;
    color: #466889;
  }

  .other-login {
    height: 100px;
    width: 100%;
    text-align: center;
    margin-top: 75px;

    .seperator {
      display: -webkit-box;
      -webkit-box-pack: justify;
      -webkit-box-align: center;
      padding: 0 15px;

      .line {
        background: #d7d7d7;
        height: 1px;
        -webkit-box-flex: 1;
      }

      .text {
        color: #999;
        font-size: 15px;
      }
    }

    .icons {
      width: 35px;
      margin: 20px auto 0;
      display: -webkit-box;
      -webkit-box-pack: justify;
      -webkit-box-align: center;

      .icon-qq {
        background: url(//www.dpfile.com/mod/app-m-login/0.1.41/css/img/oauth.png);
        width: 35px;
        height: 35px;
        font-size: 35px;
        background-size: 35px 105px;
        display: block;
        position: relative;
        -webkit-border-radius: 4.5px;
        border-radius: 4.5px;
      }
    }
  }

  .agreement {
    display: -ms-flexbox;
    display: flex;
    position: fixed;
    top: 100%;
    height: 42px;
    margin-top: -62px;
    width: 100%;
    text-align: left;
    line-height: 16px;
    -webkit-padding-start: 12px;
    padding-inline-start: 12px;
    -webkit-padding-end: 12px;
    padding-inline-end: 12px;

    .agreement-text {
      margin-right: 15px;
      color: #999;
    }

    a {
      color: #466889;
      text-decoration: none;
    }
  }
</style>